const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    picFilePath: '',
    videoFilePath: '',
    uploadText: '上传',
    canClick: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  inputTitle: function(e) {
    this.setData({
      title: e.detail.value
    });
  },
  selectPicture: function() {
    const that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          picFilePath: tempFilePaths[0]
        });
      }
    })
  },
  selectVideo: function() {
    const that = this;
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      camera: 'back',
      success(res) {
        const videoTimeLength = res.duration;
        if (videoTimeLength > 60 * 10) {
          wx.showToast({
            title: '视频长度过长,请控制视频长度在10分钟以内',
            icon: 'none',
            duration: 3000
          });
          return;
        }
        that.setData({
          videoFilePath: res.tempFilePath
        });
      }
    })
  },
  upload: function() {
    const canClick = this.data.canClick;
    if (!canClick){
      return;
    }
    const that = this;
    const openId = app.globalData.openId;
    const url = app.globalData.url;
    const baseUrl = app.globalData.baseUrl;
    const title = this.data.title;
    const picFilePath = this.data.picFilePath;
    const videoFilePath = this.data.videoFilePath;
    if (title.length === 0) {
      wx.showToast({
        title: '请填写视频标题',
        icon: 'none'
      });
      return;
    }
    if (picFilePath.length === 0) {
      wx.showToast({
        title: '请选择一张视频封面照片',
        icon: 'none'
      });
      return;
    }
    if (videoFilePath.length === 0) {
      wx.showToast({
        title: '请选择一个视频',
        icon: 'none'
      });
      return;
    }
    that.setData({
      canClick: false
    });
    //先上传图片再上传视频
    wx.uploadFile({
      url: url + '/coach/uploadVideoPicture/' + openId,
      filePath: picFilePath,
      name: 'picture',
      formData: {
        title
      },
      success: function(response) {
        const returnObj = JSON.parse(response.data);
        if (returnObj.success) {
          that.uploadVideo(returnObj.content.id);
        } else {
          that.setData({
            canClick: true
          });
          wx.showToast({
            title: returnObj.message,
            icon: 'none'
          })
        }
      }
    })
  },
  uploadVideo: function(videoId) {
    const that = this;
    const videoFilePath = this.data.videoFilePath;
    const url = app.globalData.url;
    const uploadTask = wx.uploadFile({
      url: url + '/coach/uploadVideo/' + videoId,
      filePath: videoFilePath,
      name: 'video',
      success(res) {
        const data = res.data;
        const returnObj = JSON.parse(data);
        if (returnObj.success) {
          that.setData({
            uploadText: '上传'
          });
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: '视频上传成功',
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: '视频上传失败:' + returnObj.message,
            icon: 'none',
            duration: 3000
          });
          that.setData({
            canClick: true
          });
          that.setData({
            uploadText: '上传'
          });
        }

      }
    });
    uploadTask.onProgressUpdate((res) => {
      that.setData({
        uploadText: '上传进度:' + res.progress + '%'
      });
    })
  }
})